package com.iwuyc.tools.commons.util.math;

import java.math.BigDecimal;

/**
 * 边界数据，表示无限大或者无限小
 * @author Neil
 */
public class BoundaryNumber extends BigDecimal {
    /**
     * 最小值，表示无穷小
     */
    public static final String MIN_TAG = "min";
    /**
     * 最大值，表示无穷大
     */
    public static final String MAX_TAG = "max";

    /**
     * 无穷小
     */
    public static final BigDecimal MIN_NUM = new BoundaryNumber(MIN_TAG, false);

    /**
     * 无穷大
     */
    public static final BigDecimal MAX_NUM = new BoundaryNumber(MAX_TAG, true);
    private static final long serialVersionUID = 2910914454149571890L;
    private final boolean isMax;
    private final String name;

    private BoundaryNumber(String val, boolean isMax) {
        super("0");
        this.name = val;
        this.isMax = isMax;
    }

    @Override
    public int compareTo(BigDecimal val) {
        if (isMax) {
            return 1;
        }
        return -1;
    }

    @Override
    public String toString() {
        return "BoundaryNumber(" + name + ")";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        BoundaryNumber that = (BoundaryNumber) o;
        return isMax == that.isMax;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (isMax ? 1 : 0);
        return result;
    }
}
